<?php
class Admin_CategoryController extends Zend_Controller_Action
{

	public function preDispatch()
	{}

	public function init()
	{}

	public function indexAction()
	{
		$helper = $this->_helper->getHelper('jqgrid');

	}

	public function editAction()
	{
	$form=new Myblog_Form_CategoryEdit;
	$this->view->form=$form;

		if($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost()))
		{
		$input=$this->getRequest()->getPost();
		$q=Doctrine_Query::create()
			->update('Myblog_Model_Articles')
			->set('category_name','?',$input['category_name'])
			->addWhere('category_id=?',$input['category_id']);
			$q->execute();
		$q=Doctrine_Query::create()
			->update('Myblog_Model_Categories')
			->set('category_name','?',$input['category_name'])
			->addWhere('category_id=?',$input['category_id']);
			$q->execute();
			
			$this->_helper->getHelper('FlashMessenger')
				->addMessage('Категория успешно обновлена');
			$this->_redirect('/admin/category/job/success');
		}
		if($this->getRequest()->isGet()){

		$filters=array('id'=>array('HtmlEntities','StripTags','StringTrim'));
		$validators=array('id'=>array('NotEmpty','Int'));
		$input=new Zend_Filter_Input($filters,$validators);
		$input->setData($this->getRequest()->getParams());
		$q=Doctrine_Query::create()
			->from('Myblog_Model_Categories c')
			->where('c.category_id=?',$input->id);
		
		$result=$q->fetchArray();
			if(count($result)==1){
			$this->view->form->populate($result[0]);
				$this->view->item=$result[0];
			}
		}
	}
	
	public function createAction()
	{
	$form=new Myblog_Form_CategoryCreate;
	$this->view->form=$form;

		if($this->getRequest()->isPost())
		{
			if($form->isValid($this->getRequest()->getPost()))
			{
			$values=new Myblog_Model_Categories;
			$values->fromArray($form->getValues());
			$values->save();
				if($values)
				{
				$this->_helper->getHelper('FlashMessenger')->addMessage('Категория успешно создана!');
				$this->redirect('/admin/category/job/success');
				}
			}
		}
	}

	public function deleteAction()
	{
		if($this->getRequest()->isGet())
		{
		$filters=array('id'=>array('HtmlEntities','StripTags','StringTrim'));
		$validators=array('id'=>array('NotEmpty','Int'));
		$input=new Zend_Filter_Input($filters,$validators);
		$input->setData($this->getRequest()->getParams());
			if($input->isValid()){
			$q=Doctrine_Query::create()
				->delete('Myblog_Model_Categories c')
				->whereIn('c.category_id',$input->id);
			$result=$q->execute();
		
			if($result)
				{
				$this->_helper->getHelper('FlashMessenger')->addMessage('Категория успешно удалена!');
				$this->redirect('/admin/category/job/success');
				}
			}
		}	
	}
	public function successAction()
	{
		if($this->_helper->getHelper('FlashMessenger')->getMessages()){
			$this->view->messages=$this->_helper
				->getHelper('FlashMessenger')->getMessages();
				//header("Refresh:2;/admin/category/index");
            $this->getResponse()->setHeader('Refresh', '2; URL=/admin/category/index');
			}else{
					$this->redirect('/admin');
				 }
	}
}
